package com.mycompany.project.client;

public class GameMechanics {
	GameData data = new GameData();
	int winner [] = {10,10,10};
	
	public void setButton(int i){
		data.setTableElement(i);
	}
	
	public int [] getWinnerLocations(){
		return winner;
	}
	
	public int getCurrentElement(int i){
		return data.getTableElement(i);
	}
	public int getCurrentPlay(){
		return data.getCurrentPlay();
	}
	
	public void swap(){	
		if(data.getCurrentPlay()==1){
			data.setCurrentPlay(2);
		}else{
			data.setCurrentPlay(1);
		}
		data.incGameCount();
	}
	
	public boolean checkWinner(){
		boolean result = false;
		if(checkForWinnerX()){
			data.setWinnerX();
			result=true;
		}
		if(checkForWinnerO()){
			data.setWinnerO();
			result=true;
		}
		if(data.getGameCount()==8){
			result=true;
		}
		return result;
	}
	
	public boolean checkForWinnerX(){
		boolean result = false;
		if (data.getTableElement(0)==1 && data.getTableElement(1) == 1 && data.getTableElement(2)==1){
			result = true;
			winner[0]=0;
			winner[1]=1;
			winner[2]=2;
		}
		if (data.getTableElement(3)==1 && data.getTableElement(4) == 1 && data.getTableElement(5)==1){
			result = true;
			winner[0]=3;
			winner[1]=4;
			winner[2]=5;
		}
		if (data.getTableElement(6)==1 && data.getTableElement(7) == 1 && data.getTableElement(8)==1){
			result = true;
			winner[0]=6;
			winner[1]=7;
			winner[2]=8;
		}
		if (data.getTableElement(0)==1 && data.getTableElement(4) == 1 && data.getTableElement(8)==1){
			result = true;
			winner[0]=0;
			winner[1]=4;
			winner[2]=8;
		}
		if (data.getTableElement(6)==1 && data.getTableElement(4) == 1 && data.getTableElement(2)==1){
			result = true;
			winner[0]=6;
			winner[1]=4;
			winner[2]=2;
		}
		if (data.getTableElement(0)==1 && data.getTableElement(3) == 1 && data.getTableElement(6)==1){
			result = true;
			winner[0]=0;
			winner[1]=3;
			winner[2]=6;
		}
		if (data.getTableElement(1)==1 && data.getTableElement(4) == 1 && data.getTableElement(7)==1){
			result = true;
			winner[0]=1;
			winner[1]=4;
			winner[2]=7;
		}
		if (data.getTableElement(2)==1 && data.getTableElement(5) == 1 && data.getTableElement(8)==1){
			result = true;
			winner[0]=2;
			winner[1]=5;
			winner[2]=8;
		}
		return result;
	}
	public boolean checkForWinnerO(){
		boolean result = false;
		if (data.getTableElement(0)==2 && data.getTableElement(1) == 2 && data.getTableElement(2)==2){
			result = true;
			winner[0]=0;
			winner[1]=1;
			winner[2]=2;
		}
		if (data.getTableElement(3)==2 && data.getTableElement(4) == 2 && data.getTableElement(5)==2){
			result = true;
			winner[0]=3;
			winner[1]=4;
			winner[2]=5;
		}
		if (data.getTableElement(6)==2 && data.getTableElement(7) == 2 && data.getTableElement(8)==2){
			result = true;
			winner[0]=6;
			winner[1]=7;
			winner[2]=8;
		}
		if (data.getTableElement(0)==2 && data.getTableElement(4) == 2 && data.getTableElement(8)==2){
			result = true;
			winner[0]=0;
			winner[1]=4;
			winner[2]=8;
		}
		if (data.getTableElement(6)==2 && data.getTableElement(4) == 2 && data.getTableElement(2)==2){
			result = true;
			winner[0]=6;
			winner[1]=4;
			winner[2]=2;
		}
		if (data.getTableElement(0)==2 && data.getTableElement(3) == 2 && data.getTableElement(6)==2){
			result = true;
			winner[0]=0;
			winner[1]=3;
			winner[2]=6;
		}
		if (data.getTableElement(1)==2 && data.getTableElement(4) == 2 && data.getTableElement(7)==2){
			result = true;
			winner[0]=1;
			winner[1]=4;
			winner[2]=7;
		}
		if (data.getTableElement(2)==2 && data.getTableElement(5) == 2 && data.getTableElement(8)==2){
			result = true;
			winner[0]=2;
			winner[1]=5;
			winner[2]=8;
		}
		return result;
	}
	public void resetData(){
		data.resetGameData();
		winner[0]=10;
		winner[1]=10;
		winner[2]=10;
	}

}
